package com.xmlreader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.NewsFeedClasses.NewsFeed;
import com.main.EventObject;
import com.main.EventUsersList;
import com.main.FBUser;
import com.restfb.FacebookClient;


public class XMLGenerator {
	XMLGenerator(){
	}	
	public static void generateUserXML(ArrayList<FBUser> userList){
		try {
			FileWriter fw = new FileWriter("users.xml");
			fw.write("<?xml version=\"1.0\"?>\n");
			fw.write("<users>\n");
			for(int i=0; i<userList.size(); i++){
				FBUser user = userList.get(i);
				fw.write("<user>");				
					fw.write("<name>"+user.getName()+"</name>\n");					
					fw.write("<id>" + user.getID() + "</id>\n");
					fw.write("<friends_count>"+user.getFriendList().size()+"</friends_count>\n");
					fw.write("<birth_date>"+user.getBirthdayAsDate()+"</birth_date>\n");
				fw.write("</user>\n");
			}
			fw.write("</users>\n");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void generateNewsFeedXML(FBUser user, ArrayList<NewsFeed> newsFeedList){
		try {
			FileWriter fw = new FileWriter("newsfeed.xml");
			fw.write("<?xml version=\"1.0\"?>\n");
			fw.write("<newsfeeds>\n");
			fw.write("<user name=\"" + user.getName() +"\" >");
			for(int i=0; i<newsFeedList.size(); i++){
				NewsFeed nf = newsFeedList.get(i);
				fw.write("<newsfeed>\n");
					fw.write("<id>"+nf.getNewsFeedID()+"</id>\n");					
					fw.write("<type>" + nf.getNewsFeedType() + "</type>\n");
					fw.write("<message>"+nf.getNewsFeedMessage()+"</message>\n");
					fw.write("<like_count>"+nf.getNewFeedLikeCount()+"</like_count>\n");
				fw.write("</newsfeed>\n");	
			}
			fw.write("</user>\n");
			fw.write("</newsfeeds>\n");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void generateEventsXML(FBUser user, List<EventObject> eventsList, FacebookClient facebookClient){
		try {
			FileWriter fw = new FileWriter("events.xml");
			fw.write("<?xml version=\"1.0\"?>\n");
			fw.write("<events>\n");
			fw.write("<user name=\"" + user.getName() +"\" >");
			if(eventsList != null){
				for(int i=0; i<eventsList.size(); i++){
					EventObject event = eventsList.get(i);
					EventUsersList eventUsersList = new EventUsersList(facebookClient, event.getId());
					fw.write("<event>");
						fw.write("<id>"+event.getId()+"</id>\n");					
						fw.write("<name>" + event.getName() + "</name>\n");
						fw.write("<date>"+event.getDate()+"</date>\n");
						fw.write("<attendee_count>"+eventUsersList.NumberOfAttendees()+"</attendee_count>\n");
						fw.write("<declined_count>"+eventUsersList.NumberOfDeclined()+"</declined_count>\n");
						fw.write("<invited_count>"+eventUsersList.NumberOfInvited()+"</invited_count>\n");
						fw.write("<maybe_count>"+eventUsersList.NumberOfMaybe()+"</maybe_count>\n");
						fw.write("<noreply_count>"+eventUsersList.NumberOfNoreply()+"</noreply_count>\n");
					fw.write("</event>\n");	
				}
			}	
			fw.write("</user>");
			fw.write("</events>\n");
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}